import request from '@/utils/request'

// 商家登录
export function login(data: { username: string; password: string }) {
  return request({
    url: '/merchant/login',
    method: 'post',
    data
  })
}

// 获取商家信息
export function getMerchantInfo() {
  return request({
    url: '/merchant/info',
    method: 'get'
  })
}

// 获取商品列表
export function getProducts(params: any) {
  console.log('调用商品列表API，参数:', params)
  return request({
    url: '/merchant/products',
    method: 'get',
    params
  })
}

// 添加商品
export function addProduct(data: any) {
  return request({
    url: '/merchant/products',
    method: 'post',
    data
  })
}

// 更新商品
export function updateProduct(id: number, data: any) {
  console.log(`调用更新商品API，ID: ${id}，数据:`, data)
  return request({
    url: `/merchant/products/${id}`,
    method: 'put',
    data
  })
}

// 删除商品
export function deleteProduct(id: number) {
  console.log(`调用删除商品API，ID: ${id}`)
  return request({
    url: `/merchant/products/${id}`,
    method: 'delete'
  })
}

// 更新商品状态
export function updateProductStatus(id: number, data: { status: number }) {
  console.log(`调用更新商品状态API，ID: ${id}，状态: ${data.status}`)
  return request({
    url: `/merchant/products/${id}/status`,
    method: 'put',
    params: { status: data.status }
  })
}

// 获取促销活动列表
export function getPromotions(params: any) {
  console.log('调用促销活动列表API，参数:', params)
  return request({
    url: '/merchant/promotions',
    method: 'get',
    params
  })
}

// 获取促销活动详情
export function getPromotionDetail(id: number) {
  console.log(`调用促销活动详情API，ID: ${id}`)
  return request({
    url: `/merchant/promotions/${id}`,
    method: 'get'
  })
}

// 添加促销活动
export function addPromotion(data: any) {
  console.log('调用添加促销活动API，数据:', data)
  return request({
    url: '/merchant/promotions',
    method: 'post',
    data
  })
}

// 更新促销活动
export function updatePromotion(id: number, data: any) {
  console.log(`调用更新促销活动API，ID: ${id}，数据:`, data)
  return request({
    url: `/merchant/promotions/${id}`,
    method: 'put',
    data
  })
}

// 删除促销活动
export function deletePromotion(id: number) {
  console.log(`调用删除促销活动API，ID: ${id}`)
  return request({
    url: `/merchant/promotions/${id}`,
    method: 'delete'
  })
}

// 更新促销活动状态
export function updatePromotionStatus(id: number, status: number) {
  console.log(`调用更新促销活动状态API，ID: ${id}，状态: ${status}`)
  return request({
    url: `/merchant/promotions/${id}/status`,
    method: 'put',
    params: { status }
  })
}

// 获取经营数据
export function getStatistics(params: any) {
  return request({
    url: '/merchant/statistics',
    method: 'get',
    params
  })
}

// 获取用户分析数据
export function getUserAnalysis(params: any) {
  return request({
    url: '/merchant/users/analysis',
    method: 'get',
    params
  })
}

// 修改密码
export function updatePassword(data: { oldPassword: string; newPassword: string }) {
  return request({
    url: '/merchant/password',
    method: 'put',
    data
  })
}

// 获取商家订单列表
export function getMerchantOrders(params: any) {
  console.log('调用商家订单列表API，参数:', params)
  return request({
    url: '/merchant/orders',
    method: 'get',
    params
  })
}

// 获取商家订单详情
export function getMerchantOrderDetail(orderNo: string) {
  console.log(`调用商家订单详情API，订单号: ${orderNo}`)
  return request({
    url: `/merchant/orders/${orderNo}`,
    method: 'get'
  })
}

// 商家发货
export function shipOrder(orderNo: string, trackingNo?: string) {
  console.log(`调用商家发货API，订单号: ${orderNo}, 物流单号: ${trackingNo || '无'}`)
  const data = trackingNo ? { trackingNo } : {}
  return request({
    url: `/merchant/orders/${orderNo}/ship`,
    method: 'post',
    data
  })
}

// 商家更新订单状态
export function updateOrderStatus(orderNo: string, status: number) {
  console.log(`调用商家更新订单状态API，订单号: ${orderNo}, 状态: ${status}`)
  return request({
    url: `/merchant/orders/${orderNo}/status`,
    method: 'put',
    params: { status }
  })
}

// 获取订单评价
export function getOrderReview(orderNo: string, productId: number) {
  return request({
    url: '/merchant/reviews/order',
    method: 'get',
    params: { orderNo, productId }
  })
}
